import { ref, nextTick, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
export function tableCopy({ tableData }) {
  const tableDataList = ref([])
  tableDataList.value = tableData.value || []
  // 设置复制模式
  const setCopyMode = ref(false)
  const emptyObj = ref({
    //全部列都可以粘贴
  })
  // 默认对象
  const defaultEmptyObj = ref({})
  // 当前行索引
  const currentRowIndex = ref(undefined)
  // 当前列索引
  const currentColumnIndex = ref(undefined)
  // 当前列 key
  const currentColumnKey = ref(undefined)
  // 循环存储 每行
  const rowsInfo = ref([])
  // 存储单行key 错误信息
  const itemRow = ref({})
  // 表头
  const columns = ref([])
  // 粘贴
  const pasteInfo = (e) => {
    if (!setCopyMode.value) {
      return
    }
    try {
      if (isEditRowInfo()) {
        ElMessage({
          type: 'warning',
          message: `正在行内编辑中`
        })
        return
      }
      var data = null
      var clipboardData = e.clipboardData // IE
      if (!clipboardData) {
        //chrome
        clipboardData = e.originalEvent.clipboardData
      }
      //复制过来的内容
      data = clipboardData.getData('Text')
      nextTick(() => {
        setTimeout(() => {
          //首先对源头进行解析
          var rowStrArray = data.split('\n') //拆成很多行
          rowsInfo.value = []
          emptyObj.value = {}
          defaultEmptyObj.value = {}
          for (var i = 0; i < rowStrArray.length - 1; i++) {
            var row = []
            var tdStrArray = rowStrArray[i].split('\t') //按列拆分
            for (var j = 0; j < tdStrArray.length; j++) {
              row.push(tdStrArray[j])
            }
            rowsInfo.value.push(row)
          }
          let columnslastIndex = rowsInfo.value[0].length - 1
          let columnsfirstIndex = columns.value.findIndex((item) => currentColumnKey.value == item.fieldCode)
          columns.value.map((item, index) => {
            let objkey = {
              [item.fieldCode]: undefined
            }
            if (index >= columnsfirstIndex && item.fieldCode !== '') {
              emptyObj.value = { ...emptyObj.value, ...objkey }
            }
            if (item.fieldCode !== '') {
              defaultEmptyObj.value = { ...defaultEmptyObj.value, ...objkey }
            }
          })
          let rowList = []
          for (var j = 0; j < rowsInfo.value.length; j++) {
            let temp = JSON.parse(JSON.stringify(itemRow.value))
            let num = 0
            let numFlag = JSON.parse(JSON.stringify(currentColumnIndex.value))
            for (var key in emptyObj.value) {
              if (!rowsInfo.value[j][num]) {
                break
              }
              if (currentColumnIndex.value <= numFlag) {
                temp[key] = rowsInfo.value[j][num]
                num = num + 1
              }
              numFlag = numFlag + 1
            }
            if (tableDataList.value[currentRowIndex.value + j]) {
              let item = tableDataList.value[currentRowIndex.value + j]
              Object.keys(item).forEach((key) => {
                item[key] = temp[key] == undefined ? item[key] : temp[key]
              })
            } else {
              let keyName = {}
              for (var key in defaultEmptyObj.value) {
                if (!temp[key]) {
                  keyName[key] = ''
                }
              }
              tableDataList.value.push({ ...keyName, ...temp })
            }
            rowList.push(temp)
          }
        }, 0)
      })
    } catch (err) {
    }
  }
  // 生成tableData 行
  const tableRowList = () => {
    let json = {}
    columns.value.map((item) => {
      //  item.visableNewFlag || item.visableModifyFlag || item.visableChangeFlag 判断是否可以编辑/新增/更改
      if (item.visableNewFlag || item.visableModifyFlag || item.visableChangeFlag) {
        json = {
          [item.fieldCode]: '',
          isEdit: false,
          ...json
        }
      }
    })
    for (let key in json) {
      json.isError = {
        [key]: {
          status: true,
          msg: ''
        },
        ...json.isError
      }
    }
    itemRow.value = json
  }
  // 是否正在编辑行信息
  const isEditRowInfo = () => {
    let isEdit = false
    tableDataList.value.forEach((item) => {
      if (item.isEdit) {
        isEdit = item.isEdit
      }
    })
    return isEdit
  }
  onMounted(() => {
    tableRowList()
  })
  return {
    setCopyMode,
    currentRowIndex,
    currentColumnIndex,
    currentColumnKey,
    rowsInfo,
    columns,
    itemRow,
    pasteInfo,
    tableRowList
  }
}
